package databean;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import databean.mydata;
import databean.MyRecord;

public class RecordBean {
	public RecordBean(){
		
	}
	public ResultSet getRecord(String sname) throws SQLException{
		mydata db = new mydata();
		System.out.println("testresult\n");
		ResultSet rs = null;
		try {
			String sql = "select R.recordId,R.username,R.productId,R.productname,R.amount,R.buyTime,P.valuePer"
					+ " from records R, productFund P "
					    + "where R.productId = P.productId "
					    + "AND R.username ='"+sname+"'";
			rs = db.myquery(sql);
			rs.close();
			db.closesql();
			} catch(SQLException e) {
				System.out.println(e.getMessage());
			}
		return rs;
	}
	
	public ArrayList<MyRecord> getAllRecord(String sname){
		ArrayList<MyRecord> arr = new ArrayList<MyRecord>();
		mydata db = new mydata();
		System.out.println("testresult\n");
		ResultSet rs = null;
		try {
			String sql = "select R.recordId,R.username,R.productId,R.productname,R.amount,R.buyTime,P.valuePer"
					+ " from records R, productFund P "
					    + "where R.productId = P.productId "
					    + "AND R.username ='"+sname+"'";
			rs = db.myquery(sql);
			while(rs.next()){
				int recordId = rs.getInt("recordId");
				String username = rs.getString("username");
				int productId = rs.getInt("productId");
				String productname = rs.getString("productname");
				int amount = rs.getInt("amount");
				String buyTime = rs.getString("buyTime");
				Double valuePer = rs.getDouble("valuePer");
				MyRecord mr = new MyRecord(recordId,username,productId,productname,amount,buyTime,valuePer);
				arr.add(mr);
			}
			System.out.println(arr.size());
			rs.close();
			db.closesql();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return arr;
	}
}
